package com.ybliang.plat.common.autoconfig.webapp;

import com.ybliang.plat.common.rpc.protocol.json.JSON;
import com.ybliang.plat.common.web.interceptor.AbstractPlatInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;
import java.util.Map;

/**
 * WebConfiguration
 * @author liangyb1
 */
@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {

    private Logger LOGGER = LoggerFactory.getLogger(WebConfiguration.class);

    private static final String HTTP_ENCODING = "UTF-8";

    @Bean
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
        filter.setEncoding(HTTP_ENCODING);
        filter.setForceRequestEncoding(true);
        filter.setForceResponseEncoding(true);
        return filter;
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        Map<String, AbstractPlatInterceptor> platInterceptorBeans =
                getApplicationContext().getBeansOfType(AbstractPlatInterceptor.class);
        platInterceptorBeans.values().stream().forEach(platInterceptorBean -> {
            registry.addInterceptor(platInterceptorBean);
            LOGGER.info("WebConfiguration assemble interceptor:{}", platInterceptorBean.toString());
        });
    }

    @Override
    public void addCorsMappings(CorsRegistry registry){
        //配置允许跨域访问的路径
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET")
                .allowedHeaders("x-requested-with,content-type")
                .allowCredentials(true);
    }

    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        /*
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        converter.setObjectMapper(mapper);
        */
        converter.setObjectMapper(JSON.getMapper());
        return converter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //将我们定义的时间格式转换器添加到转换器列表中,
        //这样jackson格式化时候但凡遇到Date类型就会转换成我们定义的格式
        converters.add(jackson2HttpMessageConverter());
    }
}
